/*
 * @Autor: dingxiaolin
 * @Date: 2020-08-07 22:11:04
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-01-05 18:07:40
 */
const ci = require("miniprogram-ci");
const { appid } = require("../../src/manifest.json")["mp-weixin"];
const { name } = require("../../src/manifest.json");
const { getFullDate } = require("../../src/utils/time");
const buildTime = getFullDate(new Date());
const stamp = (
  (Date.parse(new Date())
    .toString()
    .substr(0, 10) -
    1588262400) /
    86400 /
    7 /
    100 +
  1
).toFixed(9);
const version = stamp.substr(0, 4) + "." + stamp.substr(4);
const { noticeSucc, noticeFail } = require("./notice");
const server = process.argv[2];
if (server == "dev") {
  var rootNO = 1;
  var path = "dev";
  var desc = "测试服";
} else if (server == "master") {
  // var rootNO=30;
  var rootNO = 1;
  var path = "build";
  var desc = "正式服";
}

const project = new ci.Project({
  appid: appid,
  type: "miniProgram",
  projectPath: process.cwd() + `/dist/${path}/mp-weixin`,
  privateKeyPath:
    process.cwd() + "/config/mp-weixin/key/private.wx0f12a6d8945a08a4.key",
  ignores: ["node_modules/**/*"],
});
ci.upload({
  project,
  version,
  desc,
  setting: {
    es6: true,
  },
  robot: rootNO,
  onProgressUpdate: console.log,
})
  .then((res) => {
    console.log(res);
    console.log("上传成功");
    noticeSucc(
      `${name}${desc} Build v${version} 于 ${buildTime}发布体验版\n若与描述不符，联系开发手动切换体验版，二维码不变`
    );
  })
  .catch((error) => {
    console.log(error);
    console.log("上传失败");
    noticeFail("发布体验版失败，前端开发请重新上传");
    process.exit(-1);
  });
